﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml.Linq;
using System.Drawing;
using Entities;

namespace BIUtils
{
    public class GooglePlacesClient
    {
        private string AccessToken {get;set;}
        private readonly int SearchRadius = 500;

        public GooglePlacesClient(string accessToken)
        {
            AccessToken = accessToken;
        }

        public List<PlaceInfo> FindFashionInCoordinate(KeyValuePair<double, double> location)
        {
            string runQuery = "https://maps.googleapis.com/maps/api/place/search/xml?location={0}&radius={1}&sensor=false&key={2}&types=clothing_store";
            var locationString = string.Format("{0},{1}",location.Key, location.Value);
            runQuery = string.Format(runQuery, locationString, SearchRadius, AccessToken);
            using (var client = new WebClient())
            {
                var rslt = client.DownloadString(runQuery);
                var xDoc = XDocument.Parse(rslt);
                var places = new List<PlaceInfo>();
                foreach (var place in xDoc.Descendants("result"))
                {
                    var placeInfo = new PlaceInfo();
                    placeInfo.Name = place.Element("name").Value;
                    placeInfo.Reference = place.Element("reference").Value;
                    SetExtraPlaceInfo(placeInfo);
                    places.Add(placeInfo);
                }
                return places;
            }
        }

        private void SetExtraPlaceInfo(PlaceInfo placeInfo)
        {
            var query = "https://maps.googleapis.com/maps/api/place/details/xml?reference={0}&sensor=false&key={1}";
            var runQuery = string.Format(query, placeInfo.Reference, AccessToken);
            using (var client = new WebClient())
            {
                var rslt = client.DownloadString(runQuery);
                var xDoc = XDocument.Parse(rslt);
                var root = xDoc.Element("PlaceDetailsResponse").Element("result");
                placeInfo.Phone = root.Element("formatted_phone_number") == null ? string.Empty : root.Element("formatted_phone_number").Value;
                placeInfo.Address = root.Element("formatted_address") == null ? string.Empty : root.Element("formatted_address").Value;
                placeInfo.WebsiteUrl = root.Element("website") == null ? string.Empty : root.Element("website").Value;
                if (!string.IsNullOrEmpty(placeInfo.WebsiteUrl))
                {
                    placeInfo.WebsiteUrl = placeInfo.WebsiteUrl.ToLower().Contains("http") 
                                            ? placeInfo.WebsiteUrl 
                                            : string.Format("http://{0}", placeInfo.WebsiteUrl);
                }
            }
        }

        public string Search(string query)
        {
            string runQuery = "https://maps.googleapis.com/maps/api/place/textsearch/xml?location=40.7711329,-73.9741874&radius=7500&sensor=false&key={0}&query={1}&types=clothing_store";
            runQuery = string.Format(runQuery, AccessToken, string.Format("\"{0}\"", query));
            using (var client = new WebClient())
            {
                return client.DownloadString(runQuery);
            }
        }

        public bool IsInNewYork(string query)
        {
            var rslt = Search(query);
            var xDoc = XDocument.Parse(rslt);

            foreach (var info in xDoc.Descendants("result"))
            {
                if (info.Element("name").Value.ToLower().Contains(query.ToLower()) &&
                    (info.Element("formatted_address").Value.ToLower().Contains("new york") || info.Element("formatted_address").Value.ToLower().Contains("ny")))
                    return true;
            }
            return false;
        }

        public Point GetGecode(string address)
        {
            var query = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}", address);
            var rslt = Search(query);
            var xDoc = XDocument.Parse(rslt);
            return new Point();
        }
    }
}
